"""
from app.common.errors import ApiError
用到ApiError的時候必須要以此路徑導入,否則就不能正確在返回error中賦值code，
這是一個坑，patch_graphql處理error的時候isinstance(err.original_error, ApiError)導入路徑不一致時返回值為False，兩者都是ApiError
"""
from enum import Enum

ASSERTION_ERROR_CODE = 405
DB_ERROR_CODE = 406


class Error(Enum):
    InvalidParams = "400, 参数错误"
    AuthError = "401, 用户身份验证失败"
    NEED_LOGIN = "402, 需要重新登录"
    OBJECT_NOT_EXISTED = "403, 对象不存在"
    CANNOT_BLOCK_YOURSELF = "404, 不能拉黑自己"
    CANNOT_UNBLOCK_YOURSELF = "407, 不能取消拉黑自己"
    CANNOT_FOLLOW_YOURSELF = "408, 不能关注自己"
    CANNOT_UNFOLLOW_YOURSELF = "409, 不能取消关注自己"
    PAGINATE_LIMIT_ERROR = "410, 每页数据不得超过 {limit} 条"
    CANNOT_FOLLOW_BLOCKED_USER = "411, 不能关注已拉黑的用户"
    PERMISSION_ERROR = "412, 没有操作权限"
    CUSTOM_ERROR = "499, {err}"


class ApiError(Exception):
    def __init__(self, code_and_description: Error, **kwargs):
        code, description = code_and_description.value.split(",")
        self.code = int(code.strip())
        self.description = description.strip()
        if kwargs:
            self.description = self.description.format(**kwargs)

    def __str__(self):
        return self.description
